<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <link rel="stylesheet" href="../../lib/yzhUpload/control/css/zyUpload.css" type="text/css">
    <style>
        body {
            background-color: #ffffff;
        }
        .aui-upload-box {
            background-color: #fff;
            width: 105px;
            height: 105px;
            padding-top: 5px;
            padding-right: 5px;
        }

        .chose_pic_btn {
            width: 95px;
            height: 95px;
            position: relative;
            display: inline-block;
            background-image: url('');
            box-sizing: border-box;
            background-size: 45px 45px;
            background-position: center center;
            background-repeat: no-repeat;
            background-color: #ededed;
            margin-left: 3.3333%;
            margin-bottom: 12px;
            border-radius: 5px;
        }

        .chose_pic_btn div {
            position: absolute;
            left: 0;
            top: 0;
            opacity: 0;
            width: 100%;
            height: 100%;
        }

        .cancel {
            width: 20px;
            height: 20px;
            line-height: 19px;
            border: 1px solid #dbd9d9;
            background-color: #dbd9d9;
            border-radius: 50%;
            position: absolute; /* 父组件赋予属性relative生效 */
            right: 0px; /* 使元素处于右上角-右 */
            top: 0px; /* 使元素处于右上角-上 */
        }

        .cancel::before,
        .cancel::after {
            content: "";
            position: absolute;
            height: 14px;
            width: 2px;
            top: 3px; /* 使X处于圆中心 */
            left: 9px; /* 使X处于圆中心 */
            background-color: #353535;
        }

        .cancel::before {
            transform: rotate(45deg);
        }

        .cancel::after {
            transform: rotate(-45deg);
        }
        .zyupload {
            height: auto !important;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">客服userid</label>
        <div class="layui-input-block">
            <input type="text" name="userId" lay-verify="required" lay-reqtext="" placeholder="请填写客服的用户id" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">客服昵称</label>
        <div class="layui-input-block">
            <input type="text" name="csName" lay-verify="required" lay-reqtext="客服昵称不能为空" placeholder="请填写客服昵称" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">客服类型</label>
        <div class="layui-input-block">
            <select name="csType" lay-filter="cstype">
                <option value="测评客服">测评客服</option>
                <option value="招聘客服">招聘客服</option>
                <option value="商品/帮卖客服">商品/帮卖客服</option>
                <option value="平台客服">平台客服</option>
                <option value="样品客服">样品客服</option>
            </select>
        </div>
    </div>
    <!--<div class="layui-form-item layui-form-text">
        <label class="layui-form-label">客服二维码</label>
        <div class="layui-input-block">
            <input type="text" name="csQrCode" placeholder="请填写客服二维码地址" value="" lay-verify="required" lay-reqtext="客服二维码地址不能为空" class="layui-input">
        </div>
    </div>-->
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">客服二维码</label>
        <div class="layui-input-block">
            <div style="display: flex;flex-wrap: wrap">
                <div id="ad-images" style="display: flex;flex-wrap: wrap"></div>
                <div class="aui-upload-box" onclick="addPic()">
                    <div class="aui-upload-pic">
					<span class="chose_pic_btn">
						<div></div>
					</span>
                    </div>
                </div>
            </div>
            <div id="zy-upload" class="zyupload"></div>
            <input type="text" name="csQrCode" value="" hidden>
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">头部提示语</label>
        <div class="layui-input-block">
            <input type="text" name="topPrompt" placeholder="请填写头部提示语" value="" lay-verify="required" lay-reqtext="头部提示语不能为空" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">底部提示语</label>
        <div class="layui-input-block">
            <input type="text" name="bottomPrompt" placeholder="请填写底部提示语" value="" lay-verify="required" lay-reqtext="底部提示语不能为空" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <select name="status" lay-filter="status">
                <option value="1">启用</option>
                <option value="0">禁用</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">场景ID</label>
        <div class="layui-input-block">
            <input type="text" name="sceneId" placeholder="请填写场景ID" value="" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="../../lib/common/common.js" charset="utf-8"></script>
<script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="https://unpkg.com/cos-js-sdk-v5/dist/cos-js-sdk-v5.min.js"></script>
<!-- 引用核心层插件 -->
<script type="text/javascript" src="../../lib/yzhUpload/core/zyFile.js"></script>
<!-- 引用控制层插件 -->
<script type="text/javascript" src="../../lib/yzhUpload/control/js/zyUpload.js"></script>

<script>
    window.onload = function () {
        // 初始化插件
        $("#zy-upload").zyUpload({
            width: "650px",                 // 宽度
            height: "400px",                 // 宽度
            itemWidth: "120px",                 // 文件项的宽度
            itemHeight: "100px",                 // 文件项的高度
            url: "/upload/UploadAction",  // 上传文件的路径
            multiple: true,                    // 是否可以多个文件上传
            dragDrop: true,                    // 是否可以拖动上传文件
            del: true,                    // 是否可以删除文件
            finishDel: false,  				  // 是否在上传文件完成后删除预览
            /* 外部获得的回调接口 */
            onSelect: function (files, allFiles) {                    // 选择文件的回调方法
                console.info("当前选择了以下文件：");
                console.info(files);
                console.info("之前没上传的文件：");
                console.info(allFiles);
            },
            onDelete: function (file, surplusFiles) {                     // 删除一个文件的回调方法
                console.info("当前删除了此文件：");
                console.info(file);
                console.info("当前剩余的文件：");
                console.info(surplusFiles);
            },
            onSuccess: function (file, response) {                    // 文件上传成功的回调方法
                console.info("此文件上传成功：");
                if (isNotNull(response.url)) {
                    uploadAddToImg(response.url);
                }
            },
            onFailure: function (file) {                    // 文件上传失败的回调方法
                console.info("此文件上传失败：");
                console.info(file);
            },
            onComplete: function (responseInfo) {           // 上传完成的回调方法
                console.info("文件上传完成");
                console.info(responseInfo);
            }
        });
    }


    layui.use(['layer', 'form', 'layarea'], function () {
        var form = layui.form,
            layer = layui.layer,
            layarea = layui.layarea,
            $ = layui.$;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var index = layer.alert("确定提交吗？", {
                title: '提示'
            }, function () {
                let myMsg = layer.msg("正在提交...",{
                    icon:16,
                    time:-1
                })
                $.ajax({
                    type:"post",
                    url:'/clientele/add',
                    data:JSON.stringify(data.field),
                    dataType:'json',
                    contentType: 'application/json; charset=UTF-8',
                    success:function(req){
                        layer.msg("已添加");
                        layer.close(index);
                        // 关闭弹出层
                        setTimeout(function () {
                            layer.close(myMsg);//手动关闭
                            let iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                        }, 1500);
                    },
                    error:function(e){
                        layer.msg("添加失败");
                        layer.close(myMsg);//手动关闭
                    }
                })

            });

            return false;
        });

    });

    /**
     * 添加商品主图
     */
    function addPic() {
        layui.use(['layer', 'form'], function () {
            const layer = layui.layer
            layer.prompt({
                title: '请输入图片链接',
                btn: ['确定', '取消'],
                area: ['200px', '20px'],
                formType: 0
            }, function (text, index) {
                let csQrCode = $('input[name="csQrCode"]').val();
                if (isNotNull(text)) {
                    if (isNotNull(csQrCode)) {
                        csQrCode += "," + text;
                    } else {
                        csQrCode = text;
                    }
                    $('input[name="csQrCode"]').val(csQrCode);
                    renderImages(csQrCode);
                } else {
                    layer.msg("图片链接不能为空噢");
                }
                layer.close(index);
            });
        });
    }
    /**
     * 渲染加载的图片
     */
    function renderImages(imageLinks) {
        $("#ad-images").html("");
        if (!isNotNull(imageLinks)) return;
        let html = '';
        let imgs_arr = imageLinks.split(",");
        if (imgs_arr.length > 0) {
            for (const i in imgs_arr) {
                html += '<div style="padding: 5px 10px 15px 0px;position: relative;width: 95px;height: 95px;">' +
                    '                <img width="95px" height="95px" src="' + imgs_arr[i] + '"></img>' +
                    '                <span class="cancel" onclick="delImage(' + i + ')"></span>' +
                    '            </div>';
            }
        }
        $("#ad-images").html(html);
    }
    /**
     * 删除图片
     */
    function delImage(index) {
        let image_val = $('input[name="csQrCode"]').val();
        let new_image_val = '';
        if (isNotNull(image_val)) {
            let imgs_arr = image_val.split(",");
            if (imgs_arr.length > 0) {
                for (const i in imgs_arr) {
                    if (i == index) {
                        continue;
                    }
                    if (isNotNull(new_image_val)) {
                        new_image_val += "," + imgs_arr[i];
                    } else {
                        new_image_val = imgs_arr[i];
                    }
                }
            }
        }
        $('input[name="csQrCode"]').val(new_image_val);
        renderImages(new_image_val);
    }
    /**
     * 上传文件完成后将文件地址添加到图片列表中
     * @param url
     */
    function uploadAddToImg(url) {
        let goodsImagesVal = $('input[name="csQrCode"]').val();
        if (isNotNull(url)) {
            if (isNotNull(goodsImagesVal)) {
                goodsImagesVal += "," + url;
            } else {
                goodsImagesVal = url;
            }
            $('input[name="csQrCode"]').val(goodsImagesVal);
            renderImages(goodsImagesVal);
        }
    }
</script>
</body>
</html>
